package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bumptech.glide.Glide;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Person;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.CircleImageView;
import com.orhanobut.logger.Logger;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liux on 2016/7/11.
 */
public class PersonTrailAdapter extends BaseAdapter {

    private List<Person> PersonInfos;
    private final Context context;

    public PersonTrailAdapter(List<Person> PersonInfos, Context context) {
        this.context = context;
        this.PersonInfos = PersonInfos;
    }

    @Override
    public int getCount() {
        return PersonInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return PersonInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_person_trail, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            resetHolder(holder);
        }
        Person person = PersonInfos.get(position);
        holder.nameTv.setText(person.getName());
        holder.mobileTv.setText(person.getMobile());
        if (!Utils.isEmpty(person.getTeamName())) {
            holder.teamTv.setText(person.getTeamName());
        }  else {
            holder.teamTv.setText("无班组");
        }
        if (!Utils.isEmpty(person.getProjectCompanyName())) {
            holder.companyTv.setText(person.getProjectCompanyName());
        }  else {
            holder.companyTv.setText("无所属公司");
        }

        String headPortrait = person.getHeadPortrait();
        if(!Utils.isEmpty(headPortrait)){
            headPortrait = headPortrait.substring(0, headPortrait.lastIndexOf(".")) + "_100x100" + headPortrait.substring(headPortrait.lastIndexOf("."));
        }
        Glide.with(context).load(headPortrait).dontAnimate().placeholder(R.mipmap.icon_avater_defalut).error(R.mipmap.icon_avater_defalut).into(holder.headIv);
        return convertView;
    }


    private void resetHolder(ViewHolder viewHolder) {
        viewHolder.headIv.setImageResource(R.mipmap.detail_loading);
    }

    static class ViewHolder {
        @BindView(R.id.head_iv)
        CircleImageView headIv;
        @BindView(R.id.name_tv)
        HXTextView nameTv;
        @BindView(R.id.team_tv)
        HXTextView teamTv;
        @BindView(R.id.company_tv)
        HXTextView companyTv;
        @BindView(R.id.label_tv)
        HXTextView labelTv;
        @BindView(R.id.mobile_tv)
        HXTextView mobileTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

